package com.admin.scroller.photoorvideo.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.Toast;

import com.admin.scroller.photoorvideo.PhotoChoseActivity;
import com.admin.scroller.photoorvideo.R;
import com.admin.scroller.photoorvideo.beans.LocalPhoto;
import com.admin.scroller.photoorvideo.view.SquaredImageView;
import com.bumptech.glide.Glide;

import java.io.File;
import java.util.List;

public class BodyAdapter extends BaseAdapter {

    private List<LocalPhoto> localPhotos;
    private Context context;
    private int selectedItem=-1;

    public BodyAdapter(Context context){
        this.context=context;
    }

    public void updata(List<LocalPhoto> localPhotos){
        this.localPhotos=localPhotos;
        this.localPhotos.add(0,new LocalPhoto());
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return localPhotos==null?0:localPhotos.size();
    }

    @Override
    public Object getItem(int position) {
        return localPhotos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder=null;
        if(convertView==null){
            convertView=LayoutInflater.from(context).inflate(R.layout.layout_grild_item,null);
            holder=new Holder(convertView);
            convertView.setTag(holder);
        }else {
            holder=(Holder)convertView.getTag();
        }
        holder.setData(localPhotos.get(position));
        return convertView;
    }

    public void selectedPosition(int position){
        LocalPhoto localPhoto=localPhotos.get(position);
        boolean s=PhotoChoseActivity.viewModel.setChosePhotos(context,localPhoto.isVideo()?localPhoto.getVideoUrl():localPhoto.getPath());
        localPhotos.get(position).setSelected(s);
        notifyDataSetChanged();
    }


    private class Holder{

        SquaredImageView imageView;
        ImageView selected;

        public Holder (View view){
            imageView=view.findViewById(R.id.iv_grild_item);
            selected=view.findViewById(R.id.iv_selected);
        }

        public void setData(LocalPhoto photo){
            if(!TextUtils.isEmpty(photo.getPath())) {
                File file = new File(photo.getPath());
                Glide.with(context).load(file).into(imageView);
                selected.setVisibility(View.VISIBLE);
                if(photo.isSelected()){
                    selected.setImageResource(R.drawable.mis_btn_selected);
                }else {
                    selected.setImageResource(R.drawable.mis_btn_unselected);
                }
            }else {
                imageView.setImageResource(R.drawable.ic_take_photo);
                selected.setVisibility(View.GONE);
            }
        }

    }
}
